module.exports = {
  rules: {
    "require-react-memo": {
      meta: {
        type: "problem",
        docs: {
          description: "require React.memo for functional components",
          category: "Best Practices",
          recommended: false,
        },
        fixable: "code",
        schema: [], // no options
      },
      create(context) {
        return {
          FunctionDeclaration(node) {
            if (node.id && node.id.name) {
              const isReactComponent = context
                .getSourceCode()
                .getText(node.parent.parent)
                .includes("React.Component");
              if (
                !isReactComponent &&
                !context
                  .getSourceCode()
                  .getText(node.parent.parent)
                  .includes("React.memo")
              ) {
                context.report({
                  node,
                  message: "React functional components should use React.memo",
                  fix(fixer) {
                    return (
                      fixer.insertTextBefore(
                        node.parent.parent,
                        "const " + node.id.name + " = React.memo("
                      ) + fixer.insertTextAfter(node.parent.parent, ");")
                    );
                  },
                });
              }
            }
          },
        };
      },
    },
  },
};
